<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input
      type="text"
      class="box"
      ref="query"
      :placeholder="placeholder"
      v-model="query"
    />
    <i class="icon-dismiss" @click="clear" v-show="query"></i>
  </div>
</template>


<script type="text/ecmascript-6">
import { debounce } from 'assets/js/util'

export default {
  name: 'search-box',
  props: {
    placeholder: {
      type: String,
      default: '搜索歌曲、歌手',
    },
  },
  data() {
    return {
      query: ''
    }
  },
  created() {
    this.$watch('query', debounce(newQeury => {
      this.$emit('query', newQeury)
    }, 200))
  },
  methods: {
    setQuery(val) {
      this.query = val
    },
    clear() {
      this.query = ''
    },

    blur() {
      this.$refs.query.blur()
    }
  }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
@import '~assets/stylus/variable';

.search-box {
  display: flex;
  align-items: center;
  padding: 0 6px;
  height: 40px;
  width: 100%;
  box-sizing: border-box;
  background: $color-highlight-background;
  border-radius: 6px;

  .icon-search {
    font-size: 24px;
    color: $color-background;
  }

  .box {
    flex: 1;
    margin: 0 5px;
    line-height: 18px;
    outline: 0;
    font-size: $font-size-medium;
    color: $color-text;
    background: $color-highlight-background;

    &::placeholder {
      color: $color-text-d;
    }
  }

  .icon-dismiss {
    font-size: 16px;
    color: $color-background;
  }
}
</style>